package com.qiantu.phone.widget.setting;

import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceCustomDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.DeviceKeyChangeEvent;
import com.qiantu.phone.ui.activity.EditSceneActivityNew;
import com.qiantu.phone.ui.activity.PanelBingingActivity;
import com.qiantu.phone.ui.dialog.BottomEditDialog;
import com.qiantu.phone.ui.dialog.BottomModifyKeyNameDialog;
import com.qiantu.phone.utils.Constants;
import com.qiantu.phone.widget.btnview.OnBtnClickListener;
import com.qiantu.phone.widget.btnview.OnBtnLongClickListener;
import com.lxj.xpopup.XPopup;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import okhttp3.Call;

import static android.util.TypedValue.COMPLEX_UNIT_PX;

public class DeviceSettingModifyKeyName extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private int mRemainScanTimes;
    private AppActivity mContext;
    private List<DeviceCustomKeyBean> customKeyList;
//    private Map<String, DeviceCustomKeyBean.KeyData> keyDataMap = new HashMap<>();

    public DeviceSettingModifyKeyName(AppActivity context, DeviceBean deviceBean, int remainScanTimes,String title) {
        this(context, null, deviceBean, remainScanTimes, title);
    }

    public DeviceSettingModifyKeyName(AppActivity context, AttributeSet attrs, DeviceBean deviceBean, int remainScanTimes, String title) {
        this(context, attrs, 0, deviceBean, remainScanTimes, title);
    }

    public DeviceSettingModifyKeyName(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean, int remainScanTimes, String title) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean, remainScanTimes, title);
    }

    public DeviceSettingModifyKeyName(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean, int remainScanTimes, String title) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mRemainScanTimes = remainScanTimes;
        mContext = context;
        setLeftText(title);
        setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setRightTextSize(COMPLEX_UNIT_PX, DensityUtils.dip2px(context, 12));
        setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        showDialog();
    }

    private BottomModifyKeyNameDialog mBottomModifyKeyNameDialog;
    private void showDialog(){
        if (mBottomModifyKeyNameDialog == null){
            mBottomModifyKeyNameDialog = new BottomModifyKeyNameDialog(mContext);
            mBottomModifyKeyNameDialog.setOnBtnClickListener(new OnBtnClickListener(){
                @Override
                public void onClick(int btnID) {
                    Intent intent = new Intent(mContext, PanelBingingActivity.class);
                    intent.putExtra("panelIndex",btnID);
                    intent.putExtra("panelSerialNo",mDevice.getDeviceSerialNo());
                    mContext.startActivity(intent);
                }
            });
            mBottomModifyKeyNameDialog.setOnBtnLongClickListener(new OnBtnLongClickListener() {
                @Override
                public void onLongClick(int btnID) {
                    mEditKey = getKeyData(String.valueOf(btnID));
                    if (mEditKey == null){
                        mEditKey = new DeviceCustomKeyBean();
                        mEditKey.setDeviceSerialNo(mDevice.getDeviceSerialNo());
                        mEditKey.setKeyIndex(String.valueOf(btnID));
                    }
                    showBottomEditDialog(mEditKey.getKeyName() != null ? mEditKey.getKeyName() : "");
                }
            });
            new XPopup.Builder(getContext())
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomModifyKeyNameDialog);
            mBottomModifyKeyNameDialog.setRemainScanTimes(mRemainScanTimes);
        }
        getDeviceCustomKeyList();
        mBottomModifyKeyNameDialog.setDeviceType(mDevice.getDeviceType(),mDevice.getDeviceSerialNo(),mDevice.getRoomSerialNo());
        mBottomModifyKeyNameDialog.show();
    }

    BottomEditDialog mBottomEditDialog;
    private DeviceCustomKeyBean mEditKey;
    private void showBottomEditDialog(String text) {
        if (mEditKey == null) return;
        if (mBottomEditDialog == null) {
            mBottomEditDialog = new BottomEditDialog(mBottomModifyKeyNameDialog.getContext());
            mBottomEditDialog.setLabel(getContext().getString(R.string.key_name));
            mBottomEditDialog.setHint(getContext().getString(R.string.input_key_name_hint));
            mBottomEditDialog.setDialogClickListener(new BottomEditDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {
                    LLHttpManager.saveCustomKeyName(mContext, mEditKey, mBottomEditDialog.getText(), new HttpCallback<HttpData<List<DeviceCustomKeyBean>>>(mContext){
                        @Override
                        public void onSucceed(HttpData<List<DeviceCustomKeyBean>> result) {
                            super.onSucceed(result);
                            List<DeviceCustomKeyBean> beans = result.getData();
                            mEditKey.setKeyName(mBottomEditDialog.getText());
                            mBottomModifyKeyNameDialog.setCustomKeyName(mEditKey.getKeyIndex(), mEditKey.getKeyName());
                            for(DeviceCustomKeyBean bean : beans){
                                LLDeviceCustomDBManager.getInstance(getContext()).put(bean);
                            }
                            EventBus.getDefault().post(new DeviceKeyChangeEvent(mDevice.getDeviceSerialNo()));
                        }
                    });
                }
            });
            new XPopup.Builder(mBottomModifyKeyNameDialog.getContext())
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomEditDialog);
        }
        mBottomEditDialog.setText(text);
        mBottomEditDialog.setMaxLength(Constants.KEY_NAME_MAX_LENGTH);
        mBottomEditDialog.show();
    }

    public void getDeviceCustomKeyList(){
        customKeyList = LLDeviceCustomDBManager.getInstance(getContext()).getDataByDeviceSerialNo(mDevice.getDeviceSerialNo());
        if (mBottomModifyKeyNameDialog != null){
            mBottomModifyKeyNameDialog.initCustomKeyInfo(customKeyList);
        }
//        LLHttpManager.getDeviceCustomKeyList(mContext, mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<DeviceCustomKeyBean>>(null) {
//            @Override
//            public void onStart(Call call) {
//                //不跳出等待窗
//            }
//
//            @Override
//            public void onSucceed(HttpData<DeviceCustomKeyBean> result) {
//                customKeyList = result.getData().getDeviceCustomKeys();
////                if (customKeyList != null){
////                    for (DeviceCustomKeyBean.KeyData data : customKeyList){
////                        keyDataMap.put(data.getKeyIndex(), data);
////                    }
////                }
//                if (mBottomModifyKeyNameDialog != null){
//                    mBottomModifyKeyNameDialog.initCustomKeyInfo(customKeyList);
//                }
//            }
//
//            @Override
//            public void onEnd(Call call) {
//                //不跳出等待窗
//            }
//        });
    }

    private DeviceCustomKeyBean getKeyData(String btnID){
        if(customKeyList == null){
            return null;
        }
        for(DeviceCustomKeyBean keyData : customKeyList){
            if(keyData.getKeyIndex().equals(btnID)){
                return keyData;
            }
        }
        return null;
    }
}
